package com.servoy.r3.tests;

import java.io.StringReader;
import java.util.HashMap;

import com.servoy.r3.plugins.JRXMLDocumentProxy;

import junit.framework.Assert;
import junit.framework.TestCase;

public class JRXMLDocumentProxyTests extends TestCase {

	public JRXMLDocumentProxyTests(String name) {
		super(name);
	}

	protected void setUp() throws Exception {
		super.setUp();
	}

	protected void tearDown() throws Exception {
		super.tearDown();
	}

	public final void testGetResourceBundle() throws Exception {

		String input = "<jasperReport @0></jasperReport>";
		String[][] inputarray = {
				{ "" },
				{ JRXMLDocumentProxy.JRXML_RESOURCEBUNDLE_ATTR + "=\"\"" },
				{ JRXMLDocumentProxy.JRXML_RESOURCEBUNDLE_ATTR
						+ "=\"testresource\"" } };
		String[] expected = { "test", "test", "testresource" };

		for (int i = 0; i < inputarray.length; i++) {
			String resources = TestsUtils.fillString(input, inputarray[i]);

			JRXMLDocumentProxy dp = new JRXMLDocumentProxy(new StringReader(
					resources), "test.jrxml", new HashMap());

			String result = dp.getResourceBundle();

			// System.out.println(result);

			Assert.assertEquals(expected[i], result);
		}
	}

	public final void testGetQueryStringWithDefaultParameterValues()
			throws Exception {
		String teststr = "<jasperReport><parameter name=\"@0\" isForPrompting=\"false\" class=\"java.lang.String\">"
				+ "<defaultValueExpression ><![CDATA[\"London\"]]></defaultValueExpression>"
				+ "</parameter>"
				+ "<queryString><![CDATA[select * from customers where city = $P{@0}]]></queryString></jasperReport>";

		String[][] inputarray = { { "" }, { "city" } };

		String[] expected = { "select * from customers where city = 'London'",
				"select * from customers where city = 'London'" };

		String[] expected2 = { "select * from customers where city = $P{}",
				"select * from customers where city = $P{city}" };

		for (int i = 0; i < inputarray.length; i++) {
			String resources = TestsUtils.fillString(teststr, inputarray[i]);

			JRXMLDocumentProxy dp = new JRXMLDocumentProxy(new StringReader(
					resources), "test.jrxml", new HashMap());

			String result = dp.getQueryString(true);

			Assert.assertEquals(expected[i], result);

			String result2 = dp.getQueryString(false);

			Assert.assertEquals(expected2[i], result2);
		}
	}

	public final void testGetQueryStringWithParameterValues() throws Exception {
		String teststr = "<jasperReport><parameter name=\"@0\" isForPrompting=\"false\" class=\"java.lang.String\">"
				+ "<defaultValueExpression ><![CDATA[\"London\"]]></defaultValueExpression>"
				+ "</parameter>"
				+ "<queryString><![CDATA[select * from customers where city = $P{@0}]]></queryString></jasperReport>";

		String[][] inputarray = { { "" }, { "city" } };

		HashMap hm = new HashMap();
		hm.put("city", "valuecity");
		hm.put("test", "testvalue");

		String[] expected = { "select * from customers where city = 'London'",
				"select * from customers where city = 'valuecity'" };

		String[] expected2 = { "select * from customers where city = $P{}",
				"select * from customers where city = $P{city}" };

		for (int i = 0; i < inputarray.length; i++) {
			String resources = TestsUtils.fillString(teststr, inputarray[i]);

			JRXMLDocumentProxy dp = new JRXMLDocumentProxy(new StringReader(
					resources), "test.jrxml", hm);

			String result = dp.getQueryString(true);

			Assert.assertEquals(expected[i], result);

			String result2 = dp.getQueryString(false);

			Assert.assertEquals(expected2[i], result2);
		}
	}

	public final void testUpdateQueryStringForXML() throws Exception{

		String teststr = "<jasperReport>" +
						"<queryString ><![CDATA[select * from customers where city = 'London']]></queryString>" +
						"</jasperReport>";

		String[] inputarray = { "test.jrxml", "C:\test.jrxml" };
		
		for (int i = 0; i < inputarray.length; i++) {
		
			JRXMLDocumentProxy dp = new JRXMLDocumentProxy(new StringReader(
				teststr), inputarray[i], new HashMap());
		
			String result = dp.updateQueryStringForXML();
		
			System.out.println(result);
		}
		


		fail("Not yet implemented"); // TODO
	}
}
